package com.zoo.diannaopeijian.controller;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.domain.Staff;
import com.zoo.diannaopeijian.service.StaffService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 员工控制器
 */
@Controller
@RequestMapping(value = "/staff")
public class StaffController extends BaseController {
    @Resource
    private StaffService staffService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "staff/" + pageName;
    }

//    @GetMapping(value = "staffManaPage")
//    public String StaffManaPageRoute() {
//        return "staff/staffManaPage";
//    }
//
//    @GetMapping(value = "staffAddPage")
//    public String StaffAddPageRoute() {
//        return "staff/staffAddPage";
//    }
//
//    @GetMapping(value = "staffUpdatePage")
//    public String StaffUpdatePageRoute() {
//        return "staff/staffUpdatePage";
//    }

    @GetMapping(value = "/list")
    public ResponseEntity<Map<String, Object>> staffListGet(
            Page page,
            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
            Staff staff
    ) {
        return ResponseEntity.ok(buildPageResult(isFuzzy ? staffService.getFuzzyStaffWithPage(page, staff) : staffService.getStaffWithPage(page, staff)));
    }

    @PostMapping(value = "/")
    public ResponseEntity<String> staffPost(Staff staff) {
        staff.setStaffPwd(new SimpleHash("MD5",staff.getStaffPwd(),staff.getStaffPwdSalt(),1).toHex());

        return ResponseEntity.ok(staffService.addStaff(staff) ? success : fail);
    }

    @DeleteMapping(value = "/{staffId}")
    public ResponseEntity<String> staffDelete(@PathVariable Integer staffId) {
        return ResponseEntity.ok(staffService.deleteStaff(staffId) ? success : fail);
    }

    @PutMapping(value = "/")
    public ResponseEntity<String> staffPut(Staff staff) {
        if (!StringUtils.isEmpty(staff.getStaffPwd())) {
            staff.setStaffPwd(new SimpleHash("MD5",staff.getStaffPwd(),staffService.getStaffSaltById(staff.getStaffId()),1).toHex());
        }
        return ResponseEntity.ok(staffService.updateStaff(staff) ? success : fail);
    }
}
